package stack.easy;

import java.util.Stack;

/**
 * 使用两个栈模拟队列
 */
@SuppressWarnings("all")
public class 化栈为队 {
    class MyQueue {

        Stack<Integer> stackA;
        Stack<Integer> stackB;
        /** Initialize your data structure here. */
        public MyQueue() {
            stackA = new Stack<>();
            stackB = new Stack<>();
        }

        /** Push element x to the back of queue. */
        public void push(int x) {
            stackA.add(x);
        }

        /** Removes the element from in front of queue and returns that element. */
        public int pop() {
            while(!stackA.isEmpty()) {
                stackB.add(stackA.pop());
            }
            int temp = stackB.pop();
            while(!stackB.isEmpty()) {
                stackA.add(stackB.pop());
            }
            return temp;
        }

        /** Get the front element. */
        public int peek() {
            while(!stackA.isEmpty()) {
                stackB.add(stackA.pop());
            }
            int temp = stackB.peek();
            while(!stackB.isEmpty()) {
                stackA.add(stackB.pop());
            }
            return temp;
        }

        /** Returns whether the queue is empty. */
        public boolean empty() {
            return stackA.isEmpty();
        }
    }
}
